package com.wan.filter;

import com.wan.pojo.User;
import com.wan.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //我们Session中存储的是登录的那个用户
        User user = (User)request.getSession().getAttribute(Constants.USER_SESSION);
        // 如果Session已经是空的了，那么我们就拦截这个页面，重定向到错误页面

        if(user == null) {
            response.sendRedirect(request.getContextPath()+"/error.jsp");
        }else {
            chain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}
